<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="show" custom-class="dialogClass" @close="handleClose" :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="客户名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="text" @click="addContactInfo" v-if="flag === 'edit'">新增联系人</el-button>
      <el-table :data="customerDatalist"  v-if="flag === 'edit'">
        <el-table-column property="realName" align="center" label="姓名"></el-table-column>
        <el-table-column property="position" align="center" label="职位"></el-table-column>
        <el-table-column property="phone" align="center" label="手机号"></el-table-column>
        <el-table-column property="isAdmin" align="center" label="是否管理员"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="editCustomer(scope.row)">编辑</el-button>
            <el-popconfirm
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @confirm="deleteContactUser(scope.row)"
            >
              <el-button style="margin-left: 10px"
                         slot="reference"
                         size="mini"
                         type="danger"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="saveUserInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qs from "qs";

export default {
  name: "addCustomer",
  data() {
    return {
      show: this.showDiaolog,
      dialogTitle: '新增客户',
      dialogFormVisible: false,
      form: {
        name: '',
        address: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [
          { required: true, message: '请输入客户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入客户地址', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ]
      },
      flag: 'add'
    }
  },
  props: {
    showDiaolog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleClose() {
      this.$emit('setDialogShow', {
        name: '',
        show: false
      })
    },
    saveUserInfo(){
      let _this = this
      _this.$refs['form'].validate(valid => {
        if (valid) {
          if (_this.flag === 'add') {
            this.request.post('/uozo-alarm/user/addUser', {
              name: this.form.name,
              address: this.form.address
            }).then(() => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
            }).finally(() => {
              this.show = false
            })
          } else if (_this.flag === 'edit') {
            this.request.post('/uozo-alarm/user/addUser', {
              name: this.form.name,
              address: this.form.address
            }).then(() => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            }).finally(() => {
              this.dialogFormVisible = false
              this.getUserList()
            })
          }
        } else {
          return false;
        }
      })
    },
  }
}
</script>

<style scoped>

</style>